<template>
  <div v-if="visible">
    <transition name="modal">
      <div
          v-if="visible"
          class="modal-overlay"
          @click="handleOverlayClick"
          @keydown.esc="closeModal"
          tabindex="0"
      >
        <div
            class="modal-container"
            @click.stop
            role="dialog"
            aria-modal="true"
        >
          <div class="modal-header" v-if="$slots.header || title">
            <slot name="header">
              <h2 class="modal-title">{{ title }}</h2>
            </slot>
            <button
                class="modal-close-button"
                @click="closeModal"
                aria-label="关闭弹窗"
            >
              <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <line x1="18" y1="6" x2="6" y2="18"></line>
                <line x1="6" y1="6" x2="18" y2="18"></line>
              </svg>
            </button>
          </div>

          <div class="modal-body">
            <slot></slot>
          </div>

          <div class="modal-footer" v-if="$slots.footer">
            <slot name="footer"></slot>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'Modal',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    maskClosable: {
      type: Boolean,
      default: true
    },
    escClosable: {
      type: Boolean,
      default: true
    }
  },
  watch: {
    visible: {
      handler(newVal) {
        if (newVal) {
          document.body.style.overflow = 'hidden'
          this.$nextTick(() => {
            this.$el.focus()
          })
        } else {
          document.body.style.overflow = ''
        }
      },
      immediate: true
    }
  },
  mounted() {
    document.addEventListener('keydown', this.handleEscKey)
  },
  beforeDestroy() {
    document.removeEventListener('keydown', this.handleEscKey)
    document.body.style.overflow = ''
  },
  methods: {
    closeModal() {
      this.$emit('update:visible', false)
      this.$emit('close')
    },
    handleOverlayClick() {
      if (this.maskClosable) {
        console.log(123)
        this.closeModal()
      }
    },
    handleEscKey(event) {
      if (event.key === 'Escape' && this.escClosable && this.visible) {
        this.closeModal()
      }
    }
  }
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  padding: 20px;
  box-sizing: border-box;
}

.modal-container {
  background: white;
  border-radius: 12px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  width: 789px;
  height: 550px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.modal-header {
  padding: 24px 24px 0 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #e5e7eb;
  padding-bottom: 16px;
}

.modal-title {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
  color: #1f2937;
}

.modal-close-button {
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  border-radius: 6px;
  color: #6b7280;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-close-button:hover {
  background-color: #f3f4f6;
  color: #374151;
}

.modal-body {
  background: url("../../assets/img/切图/下钻弹窗.png") no-repeat center center;
  padding: 24px;
  flex: 1;
  overflow-y: auto;
}

.modal-footer {
  padding: 0 24px 24px 24px;
  border-top: 1px solid #e5e7eb;
  padding-top: 16px;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 动画效果 */
.modal-enter-active,
.modal-leave-active {
  transition: all 0.3s ease;
}

.modal-enter,
.modal-leave-to {
  opacity: 0;
}

.modal-enter .modal-container,
.modal-leave-to .modal-container {
  transform: scale(0.9);
  opacity: 0;
}

.modal-enter-active .modal-container,
.modal-leave-active .modal-container {
  transition: all 0.3s ease;
}

/* 响应式设计 */
@media (max-width: 640px) {
  .modal-overlay {
    padding: 16px;
  }

  .modal-header,
  .modal-body,
  .modal-footer {
    padding-left: 20px;
    padding-right: 20px;
  }

  .modal-container {
    margin: 0;
    max-height: 95vh;
  }
}
</style>